package mesh

import (
	v12 "k8s.io/api/core/v1"
	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/runtime/schema"
)

var Schema = schema.GroupVersionResource{
	Group:    "king.io",
	Version:  "v1",
	Resource: "meshes",
}

var SchemeGroupVersion = schema.GroupVersion{Group: "king.io", Version: "v1"}

var (
	SchemaBuilder      = runtime.NewSchemeBuilder(addKnownTypes)
	localSchemeBuilder = &SchemaBuilder
	AddToScheme        = SchemaBuilder.AddToScheme
)

type MeshStartStatus string

const (
	BUILD MeshStartStatus = "build"
	READY                 = "ready"
	FAIL                  = "fail"
)

type Mesh struct {
	v1.TypeMeta   `json:",inline"`
	v1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
	Spec          MeshSpec   `json:"spec,omitempty"`
	Status        MeshStatus `json:"status,omitempty"`
}

func (m *Mesh) DeepCopyObject() runtime.Object {
	if c := m.DeepCopy(); c != nil {
		return c
	}
	return nil
}

func (in *Mesh) DeepCopy() *Mesh {
	if in == nil {
		return nil
	}
	out := new(Mesh)
	in.DeepCopyInto(out)
	return out
}

func (in *Mesh) DeepCopyInto(out *Mesh) {
	*out = *in
	out.TypeMeta = in.TypeMeta
	in.Spec.DeepCopyInto(&out.Spec)
	in.Status.DeepCopyInto(&out.Status)
	return
}

type MeshSpec struct {
	Template  *v12.PodTemplateSpec `json:"template"`
	Resources []Resource           `json:"resources"`
	Count     *int32               `json:"count"`
	Port      *int32               `json:"port"`
	Version   *string              `json:"version"`
}

func (in *MeshSpec) DeepCopyInto(out *MeshSpec) {
	*out = *in
	if in.Template != nil {
		in, out := &in.Template, &out.Template
		*out = new(v12.PodTemplateSpec)
		**out = **in
	}
	if in.Count != nil {
		in, out := &in.Count, &out.Count
		*out = new(int32)
		**out = **in
	}
	if in.Port != nil {
		in, out := &in.Port, &out.Port
		*out = new(int32)
		**out = **in
	}
	if in.Resources != nil {
		in, out := &in.Resources, &out.Resources
		*out = make([]Resource, len(*in))
		for i := range *in {
			(*in)[i].DeepCopyInto(&(*out)[i])
		}
	}
	if in.Version != nil {
		in, out := &in.Version, &out.Version
		*out = new(string)
		**out = **in
	}
	return
}

func (in *MeshSpec) DeepCopy() *MeshSpec {
	if in == nil {
		return nil
	}
	out := new(MeshSpec)
	in.DeepCopyInto(out)
	return out
}

type MeshStatus struct {
	Links  map[string]interface{} `json:"links"`
	Status *string                `json:"status"`
}

func (in *MeshStatus) DeepCopyInto(out *MeshStatus) {
	*out = *in
	if in.Links != nil {
		in, out := &in.Links, &out.Links
		*out = make(map[string]interface{})
		for k, v := range *in {
			(*out)[k] = v
		}
	}

	if in.Status != nil {
		in, out := &in.Status, &out.Status
		*out = new(string)
		**out = **in
	}
	return
}

func (in *MeshStatus) DeepCopy() *MeshStatus {
	if in == nil {
		return nil
	}
	out := new(MeshStatus)
	in.DeepCopyInto(out)
	return out
}

type Resource struct {
	Protocol *string `json:"protocol"`
	NetWork  *string `json:"network"`
	Timeout  *int32  `json:"timeout"`
	Path     *string `json:"path"`
	Port     *int32  `json:"port"`
	Version  *string `json:"version"`
}

func (in *Resource) DeepCopyInto(out *Resource) {
	if in.Protocol != nil {
		in, out := &in.Protocol, &out.Protocol
		*out = new(string)
		**out = **in
	}
	if in.NetWork != nil {
		in, out := &in.NetWork, &out.NetWork
		*out = new(string)
		**out = **in
	}
	if in.Path != nil {
		in, out := &in.Path, &out.Path
		*out = new(string)
		**out = **in
	}
	if in.Timeout != nil {
		in, out := &in.Timeout, &out.Timeout
		*out = new(int32)
		**out = **in
	}
	if in.Port != nil {
		in, out := &in.Port, &out.Port
		*out = new(int32)
		**out = **in
	}
	if in.Version != nil {
		in, out := &in.Version, &out.Version
		*out = new(string)
		**out = **in
	}
	return
}

func addKnownTypes(scheme *runtime.Scheme) error {
	scheme.AddKnownTypes(SchemeGroupVersion,
		&Mesh{},
	)

	v1.AddToGroupVersion(scheme, SchemeGroupVersion)
	return nil
}
